const db = uniCloud.database();
let { operation } = uniCloud.importObject("utilsObj", {
	customUI: true // 取消自动展示的交互提示界面
});

//获取富文本内的图片url地址
export const getImgSrc = (richtext, num = 9) => {
	let imgList = [];
	richtext.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
		imgList.push(capture);
	});
	imgList = imgList.slice(0, num)
	return imgList;
}


// a55ef64f48429427ed4e209d8d7a7033 获取所在省市
export const getProvince = async () => {
	let cityObj = {
		time: Date.now()
	}
	let City = uni.getStorageSync("cityHistory")

	if (City) {
		return City
	} else {
		let result = await uni.request({
			url: "https://restapi.amap.com/v3/ip?key=a55ef64f48429427ed4e209d8d7a7033",
		})
		if (typeof (result[1].data.city) == "string") {
			cityObj.city = result[1].data.city
			uni.setStorageSync("cityHistory", cityObj)
			return result[1].data.city
		} else {
			return '火星'
		}
	}
}


// 点赞操作
export const updateLike = async (editId) => {
	let count = await db
		.collection("quanzi_like")
		.where(`article_id=="${editId}"&&user_id==$cloudEnv_uid`)
		.count();
	
	if (count.result.total) {
		let { result } = await db
			.collection("quanzi_like")
			.where(`article_id=="${editId}"&&user_id==$cloudEnv_uid`)
			.remove();
		operation("quanzi_article", "like_count", editId, -1);
	} else {
		let { result } = await db.collection("quanzi_like").add({
			article_id: editId
		});
		operation("quanzi_article", "like_count", editId, 1);
	}
}